<template>
  <div class="login_container">
    欢迎
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="logo" />
      </div>
      <el-form
        class="login_form"
        :model="loginForm"
        label-width="100px"
        :rules="loginRules"
        ref="loginFormRef"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            @focus="usernamefocus"
            @keyup.enter.native="jump"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            ref="passwordFormRef"
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-user" type="primary" @click="login"
            >登录</el-button
          >
          <el-button icon="el-icon-user" type="primary" @click="resetForm"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  // data () {return {xxx:yyy}}
  data() {
    return {
      loginForm: {
        // 用于提交到后台的数据
        username: '',
        password: ''
      },
      loginRules: {
        // 做数据验证规则定义
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 18, message: '长度在 3 到 18 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 18, message: '长度在 4 到 18 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      console.log('~~~~~~~~~~~~~~~~~~~')
      // this 是谁
      console.log(this)
      console.log(this.loginForm)
      // 发送数据到后台去，还要做指定数据校验
      this.$refs.loginFormRef.validate(async (valid) => {
        console.log(valid) // 布尔值，表示成功与否
        // console.log(obj) // 失败时返回失败规则，成功时返回{}
        if (valid) {
          // 成功怎么办 将this.loginForm发到后端，谁来做，axios
          const res = await this.$http.post('login/', this.loginForm) // axios.post返回一个promise ,json
          console.log('^^111^^^', res, '^^^^^')
          const { data: response } = res
          console.log(response)
          console.log(response.code)
          if (response.code) {
            // 不为0或者未定义则失败了
            return this.$message.error(response.message)
          }
          this.$message({
            message: '登录成功~',
            type: 'success'
          })
          window.localStorage.setItem('token', response.access)
          this.$router.push('/home') // 实现跳转
        } else {
          // 不管了
        }
      })
    },
    usernamefocus() {
      console.log('~~~~~~~~~username聚焦了~~~~~~~~~~')
    },
    jump() {
      this.$refs.passwordFormRef.focus()
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  // position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 35%;
    }
  }
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>
